home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / 80x0393.zip / STRING.ASM < prev    next >
Assembly Source File  |  1992-09-30  |  3KB  |  117 lines

  1. comment #
  2.  
  3.    STRING.ASM
  4.    Finding strings in memory
  5.  
  6.    Author: Dave Walker
  7.    placed in public domain
  8.    
  9.   The actual search is quite easy, due to the magical "rep cmpsb"
  10.   instruction. The biggest problem is normalizing the addresses. 
  11.   If you are starting with an arbitrary address, just do this:
  12.  
  13.         While (offset >= 10h)
  14.           offset = offset-10h
  15.           segment = segment+1
  16. #
  17.  
  18.             IDEAL
  19.             MODEL   TINY
  20.  
  21.             CODESEG
  22.             ORG     100h
  23.  
  24. start:      mov     dx,OFFSET askmsg         ;Ask user for string
  25.             mov     ah,9
  26.             int     21h
  27.  
  28.             mov     dx,OFFSET inbuf          ;Get input
  29.             mov     ah,0ah
  30.             int     21h
  31.  
  32.             xor     ax,ax                    ;Init target to 0:0
  33.             mov     es,ax
  34.             mov     di,ax
  35.             cld
  36.  
  37. mainloop:   mov     ch,0                     ;Init length
  38.             mov     cl,[inbuf+1]
  39.             mov     si,OFFSET string         ;Init source
  40.  
  41.             push    es                       ;Perform search
  42.             push    di
  43.             rep cmpsb
  44.             pop     di
  45.             pop     es
  46.             jz      foundit
  47.  
  48. bump:       inc     di                       ;Next mem location
  49.             cmp     di,16                    ;Past paragraph boundary?
  50.             jb      mainloop
  51.             sub     di,16                    ;If so, adjust DI and ES
  52.  
  53.             push    es                       ;INC ES (grrr...)
  54.             pop     ax
  55.             inc     ax
  56.             push    ax
  57.             pop     es
  58.  
  59.             cmp     ax,0a000h                ;Did we pass 640k?
  60.             jb      mainloop
  61.  
  62.             mov     ax,4c00h                 ;Bye-bye
  63.             int     21h
  64.  
  65. foundit:    mov     ax,es                    ;Convert segment to ASCII
  66.             mov     dx,OFFSET foundaddr
  67.             call    binhex
  68.             mov     ax,di                    ;Convert offset to ASCII
  69.             mov     dx,OFFSET foundaddr+5
  70.             call    binhex
  71.             mov     dx,OFFSET foundmsg       ;Tell user where we found
  72.             mov     ah,9                     ;  the string
  73.             int     21h
  74.             jmp     bump
  75.  
  76. PROC        binhex                           ;Convert AX to ASCII and
  77.             push    ax                       ;  store result at [DS:DX].
  78.             push    bx                       ;  All regs (including
  79.             push    cx                       ;  DX) are preserved.
  80.             push    dx
  81.             mov     bx,dx
  82.             mov     dl,ah
  83.             mov     cl,4
  84.             shr     dl,cl
  85.             call    bh10
  86.             mov     dl,ah
  87.             and     dl,15
  88.             call    bh10
  89.             mov     dl,al
  90.             shr     dl,cl
  91.             call    bh10
  92.             mov     dl,al
  93.             and     dl,15
  94.             call    bh10
  95.             pop     dx
  96.             pop     cx
  97.             pop     bx
  98.             pop     ax
  99.             ret
  100.  
  101. bh10:       cmp     dl,10
  102.             jb      bh11
  103.             add     dl,7
  104. bh11:       add     dl,'0'
  105.             mov     [bx],dl
  106.             inc     bx
  107.             ret
  108. ENDP
  109.  
  110. askmsg      db      'Enter search string : $'
  111. foundmsg    db      13,10,'String found at '
  112. foundaddr   db      'ssss:oooo','$'
  113. inbuf       db      253,?
  114. string      db      253 dup (?)
  115.  
  116.             END     start
  117.